Repository navigation
fix(storage): honor GOOGLE_CLOUD_UNIVERSE_DOMAIN in the REST client - #19248
shradhakatyal wants to merge 8 commits into
Conversation
storage.Client read the universe domain only from client_options. A workload that set only GOOGLE_CLOUD_UNIVERSE_DOMAIN got a googleapis.com client. Resolve the universe domain from client_options first, then from the environment variable. Anonymous credentials are now accepted in every universe, so the emulator and create_anonymous_client keep working when a non-default universe is configured. [Generated-by: AI]
There was a problem hiding this comment.
Code Review
This pull request introduces support for the GOOGLE_CLOUD_UNIVERSE_DOMAIN environment variable to configure the universe domain in the Google Cloud Storage client. It adds a helper function to retrieve and validate this override, updates the Client initialization to fall back to this environment variable when universe_domain is not provided in client_options, and updates credential validation to allow AnonymousCredentials in any universe. Comprehensive unit tests and a test fixture to clear the environment variable were also added. The review feedback suggests using mock.patch.dict instead of mock.patch when mocking os.environ in the tests to ensure compatibility and safety.
|
/gcbrun (0407fbe) |
…tials Blob.generate_signed_url, Bucket.generate_signed_url and Client.generate_signed_post_policy_v4 accept credentials for a single call. Nothing compared the universe domain of those credentials with the universe domain of the client. The result could be a signed URL or POST policy for the default Storage endpoint of one universe domain, signed by an identity from another. That endpoint does not accept it. Raise ValueError early when the credentials sign the result, report a non-default universe domain that differs from the client's, and the result uses the default Storage endpoint of the client's universe domain. Credentials that report the default universe domain are not compared, because credentials also report that value when no universe domain was set on them. The check is skipped for a custom endpoint, a bucket-bound hostname or the emulator, and when an access token and a service account email sign the result. Client.generate_signed_post_policy_v4 now also passes the client's universe domain when it signs with an access token and a service account email, as Blob.generate_signed_url does. [Generated-by: AI]
Use mock.patch.dict("os.environ", ..., clear=True) in the environment
variable tests this change adds. It keeps os.environ as the standard
mapping object instead of replacing it with a plain dict.
[Generated-by: AI]
|
As joint maintainers of the various repos in google-cloud-python there are some benefits on aligning on centralized functions, tests, and approaches, where it makes sense. With the rush to update all the libraries to handle universe-domain, we have a lot of changes being recommended across all of google-cloud-python. To that end, the Cloud Python Team has put together a catalog of functions, environment variables, and such that will be commonly used across the ecosystem. Most of the functions are centralized where possible in We noticed
I know that you have better context on this than I do, so if any of the suggestions above are wrong, please leave a comment here. Gemini will pick them up and learn from them during the next sync sweep. Note
If you see an issue with an item in the catalog.py, please suggest a change so that we can all benefit. |
|
/gcbrun (7aff459) |
…universe mirror Replace `_get_universe_domain_override` with `_resolve_client_universe`, a local mirror of the shared `resolve_client_universe` helper. The mirror has the `TODO(universe-domain)` header, so it can be replaced by `google.api_core.universe.resolve_client_universe` later. The local copy strips surrounding whitespace, because `determine_domain` in older google-api-core releases returns the value unstripped. `Client._universe_domain` stays `None` for the default universe unless `client_options` names it, so the default endpoint handling is unchanged. Behavior changes: - An empty or whitespace-only `client_options.universe_domain` raises `EmptyUniverseError`. - Surrounding whitespace in `client_options.universe_domain` is stripped. - `client_options` that is not a `ClientOptions`, `Mapping`, or `None` raises `TypeError`. [Generated-by: AI]
|
Done. Two notes on the mirror:
An empty Co-authored by AI Agent |
|
/gcbrun (0f79a76) |
Address review comments on the universe domain change. - `Client.__init__` converts a `client_options` mapping that is not a `dict` to a `dict`, so the constructor accepts every type that `_resolve_client_universe` accepts. - `Client.__init__` matches the default universe without regard to case. `GOOGLE_CLOUD_UNIVERSE_DOMAIN=GoogleApis.com` keeps the default endpoint. - The `TODO(universe-domain)` header of `_resolve_client_universe` names its tracking bug and issue. - Tests with several cases use `pytest.mark.parametrize`. They are in plain pytest classes, because `unittest.TestCase` does not support it. - `tests/unit/test_bucket.py` has one signing credentials helper. [Generated-by: AI]
…iverse `Blob.generate_signed_url`, `Bucket.generate_signed_url` and `Client.generate_signed_post_policy_v4` raise `ValueError` when the per-call credentials report a universe domain that differs from the client's universe domain. The tests pass `universe_domain_credential` to the default `storage_client`. They are skipped when `TEST_UNIVERSE_DOMAIN_CREDENTIAL` is not set, and when the client does not use the default storage endpoint. [Generated-by: AI]
|
/gcbrun (77dc85f) |
Summary
storage.Clientnow honors theGOOGLE_CLOUD_UNIVERSE_DOMAINenvironment variable, and the signing methods validate the universe domain of credentials passed for a single call.Before this change, the REST client read the universe domain only from
client_options.universe_domain. A workload that set only the environment variable got agoogleapis.comclient.GrpcClientandAsyncGrpcClientalready read the variable since #19188.Changes
_resolve_client_universe()in_helpers.py. It is a local mirror of the sharedresolve_client_universehelper and carries the# TODO(universe-domain):header, so it can be replaced bygoogle.api_core.universe.resolve_client_universelater. The local copy also strips surrounding whitespace, becausedetermine_domainin oldergoogle-api-corereleases returns the value unstripped.Client.__init__resolves the universe domain with that helper. Precedence isclient_options.universe_domain, then the variable, thengoogleapis.com.Client._universe_domainstaysNonewhen the resolved value is the default andclient_optionsdid not name it. The default endpoint and mTLS handling are unchanged for that case. The default universe is matched without regard to case, soGOOGLE_CLOUD_UNIVERSE_DOMAIN=GoogleApis.comkeeps working.Client.__init__converts aclient_optionsmapping that is not adictto adict, so the constructor accepts every type the resolver accepts.AnonymousCredentials, which carry no token._validate_signing_credentials_universe_domain()in_helpers.py.Blob.generate_signed_url,Bucket.generate_signed_urlandClient.generate_signed_post_policy_v4call it for credentials passed per call.Client.generate_signed_post_policy_v4passes the client's universe domain when it signs withaccess_tokenandservice_account_email, asBlob.generate_signed_urldoes. fix(storage): pass universe_domain in generate_signed_post_policy_v4 #19243 contains the same change, so either merge order works.tests/unit/conftest.pythat clears the variable, so the runner's environment cannot change test results.Behavior Changes
The first three apply only when
GOOGLE_CLOUD_UNIVERSE_DOMAINis set.Credentials whose
universe_domaindiffers from the variable now raiseValueErrorat construction. API key credentials are one example, because they reportgoogleapis.com.An empty or whitespace-only value raises
EmptyUniverseError(aValueError), as it does inGrpcClientand the generated clients.A value of
googleapis.comchanges nothing.An empty or whitespace-only
client_options.universe_domainraisesEmptyUniverseError. An empty string was ignored before.Surrounding whitespace in
client_options.universe_domainis stripped. A padded value raised a credential mismatchValueErrorbefore.client_optionsthat is not aClientOptionsinstance, aMapping, orNoneraisesTypeError.client_optionscan be anyMapping, for example atypes.MappingProxyType. A mapping other than adictraisedAttributeErrorbefore.AnonymousCredentialsare accepted with any universe domain.Client(client_options={"universe_domain": ...}, credentials=AnonymousCredentials())raisedValueErrorbefore and now works. The same applies toSTORAGE_EMULATOR_HOSTcombined with a non-default universe.Blob.generate_signed_url,Bucket.generate_signed_urlandClient.generate_signed_post_policy_v4raiseValueErrorwhen all of these hold:credentialssign the result.The default endpoint of a universe domain does not accept such a result, so the call now fails early.
Not Changed
client_options.api_endpointstill reportsgoogleapis.comas its universe domain. Setclient_options.universe_domainor the environment variable for a non-default universe.googleapis.com. Credentials also report that value when no universe domain was set on them.bucket_bound_hostname, or anapi_access_endpointthat is not the default endpoint of the client's universe domain. Anapi_access_endpointequal to that default endpoint is checked.access_tokenandservice_account_emailare passed, because thecredentialsobject does not sign in that case.AttributeError.Testing
ClientOptionsand mapping inputs, an unset variable, the default universe, precedence, surrounding whitespace, empty values, and unsupportedclient_optionstypes.client_optionsprecedence, empty and paddedclient_options.universe_domain, the default universe named inclient_optionsor in mixed case in the variable, non-dictmappings,api_endpoint, mTLS, the emulator, and anonymous credentials.pytest.mark.parametrize. They are in plain pytest classes, becauseparametrizeis not supported in theunittest.TestCaseclasses of these files.tests/system/test__signing.pypassuniverse_domain_credentialto the default client for the three signing methods and expectValueError. They are skipped whenTEST_UNIVERSE_DOMAIN_CREDENTIALis not set, and when the client does not use the default storage endpoint. They need no bucket and make no network call.